﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entities.IRepository;
using BLToolkit.Data;
using BLToolkit.Data.Linq;
using Entities;

namespace DAL.Admin
{
  public  class BLTGiaiDauRepository:ConnectionStringBL, IGiaiDauRepository
    {
      //private DbManager db = new DbManager("QuanLiBongDa");
       
 
        public int ThemGiaiDau(GiaiDau bp)
        {

            try
            {
              var u=  db.InsertWithIdentity(bp);
              db.CommitTransaction();
              return Convert.ToInt32(u);

            }
            catch (Exception)
            {
                db.RollbackTransaction();
                return -1;
            } 
        }

        public bool CapNhapGiaiDau(GiaiDau bp)
        {
            try
            {
              int kq=  db.Update(bp);
              db.CommitTransaction();
              return kq>0;
            }
            catch (Exception)
            {

                db.RollbackTransaction();
                return false;
            }
 
        }

        public bool XoaGiaiDau(GiaiDau bp)
        {
            try
            {
                int kq = db.Delete(bp);
                db.CommitTransaction();
                return kq > 0;
            }
            catch (Exception)
            {

                db.RollbackTransaction();
                return false;
            }
 
        }

        public GiaiDau LayTheoId(int id)
        {
            return db.GetTable<GiaiDau>().SingleOrDefault(x => x.MaGiaiDau == id);
        }

        public GiaiDau LayChiTietTheoId(int id)
        {
            throw new NotImplementedException();
        }

        public List<GiaiDau> LayTatCa()
        {
            return db.GetTable<GiaiDau>().OrderByDescending(y => y.ThoiGianBatDau).ToList();
        }


        public List<GiaiDau> LayTenGiaiDau(string tengd)
        {
            return db.GetTable<GiaiDau>().Where(x => x.TenGiaiDau == tengd).ToList();
        }
    }
}
